#include "Command/ZigbeeProtocol/Zigbee3ControlPointListCmd.h"

Zigbee3ControlPointListCmd::Zigbee3ControlPointListCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::ZigbeeControlProtocol;
    this->m_functionCode = FunctionCode;
}

Package Zigbee3ControlPointListCmd::toPackage()
{
    Package package;
    QByteArray body;
    quint8 Cmdhand=0x51;
    quint8 Cmdend=0x15;
    quint8 Cmdport=0x01;
    quint16 Cmdorder=0;

    body.clear();
    body.append(Cmdhand);
    body.append(m_zigbee.nwk());
    body.append(Cmdport);

    if(m_zigbee.type()==2)
    {
        qDebug()<<"type right";
        Cmdorder=0x02;
        body.append(Cmdorder);
        Cmdorder=0x01;
        body.append(Cmdorder);
        quint8 valueData=0;
        foreach(const ControlPointStruct &controlPoint,m_zigbee.pointList())
       {

            if(m_zigbee.pointValue().size()==0)
            {
              qDebug()<<"==0 value.......................................................";
                switch (controlPoint.value())
                {
                case 0x00:valueData=0x01;

                    break;
                case 0x01:valueData=0x00;

                    break;
                case 0x02:valueData=0x02;

                    break;
                default:valueData=0x02;
                    break;
                }
             }
            body.append(valueData);
            qDebug()<<"valuedata="<<(valueData);
            break;
        }
    }
    else if(m_zigbee.type()==5)
    {
        qDebug()<<"men suo type right";
        Cmdorder=0x00;
        body.append(Cmdorder);
        Cmdorder=0x00;
        body.append(Cmdorder); quint8 valueData=0;
        foreach(const ControlPointStruct &controlPoint,m_zigbee.pointList())
       {
        switch (controlPoint.value())
        {
        case 0x01:valueData=0xf0;break;
        }
       }
        body.append(valueData);
        qDebug()<<"valuedata="<<(valueData);
    }
    else if(m_zigbee.type()==6)
    {
          qDebug()<<"Type==6------------------------";
         Zigbee3InfraredXml *zigbee3InfraredXml =Zigbee3InfraredXml::getInstance();
          foreach(const Zigbee3InfraredStruct &InfraredStruct,zigbee3InfraredXml->InfraredList)
          {
              if(InfraredStruct.m_nwk==zigbee().nwk())
              {
                  qDebug()<<"nwk is right now";
                  foreach(const ControlPointStruct &controlPoint,m_zigbee.pointList())
                 {
                      if(controlPoint.value()==InfraredStruct.m_PointValue)
                      {
                          qDebug()<<"Value is right now ";
                          QString NWK=InfraredStruct.m_nwk;
                          Package package;
                          package.setBody(InfraredStruct.m_data);
                          package.setFunction(0xD0);
                          QByteArray InfraredData=zigbee3InfraredXml->generateData(package);
                          QByteArray ZBCmd;

                         ZBCmd.append(0x51);
                       //  ZBCmd.append(QByteArray::fromHex(NWK.toLatin1()));
                         ZBCmd.append(InfraredStruct.m_nwk);
                         ZBCmd.append(0x01);
                         ZBCmd.append((char)0x00);
                         ZBCmd.append((char)0x00);
                         ZBCmd.append(0X02);
                         ZBCmd.append(0x0c);
                         ZBCmd.append(0X41);
                         ZBCmd.append(0X42);
                         ZBCmd.append(InfraredData.size());
                         ZBCmd.append(InfraredData);
                         ZBCmd.append(0x15);
                         qDebug()<<"ZBCMD="<<ZBCmd;
                         package.setBody(ZBCmd);
                         package.setFunction(FunctionCode);
                         return package;
                      }
                 }

              }
          }

    }
    body.append(Cmdend);
    package.setFunction(FunctionCode);
    package.setBody(body);
    return package;
}

void Zigbee3ControlPointListCmd::unPackage(Package package)
{
    Q_UNUSED(package);
}
Zigbee3Struct Zigbee3ControlPointListCmd::zigbee() const
{
    return m_zigbee;
}

void Zigbee3ControlPointListCmd::setZigbee(const Zigbee3Struct &zigbee)
{
    m_zigbee = zigbee;
}
